package pers.lolicer.redtidelibrary.repository.dao

import androidx.room.Dao
import androidx.room.Query
import pers.lolicer.redtidelibrary.repository.entity.BookEntity

@Dao
interface BookDao {
    // @Query("SELECT uri FROM book ORDER BY time_last_open LIMIT 10")
    // fun getRecentlyBookUris(): List<String>;

//    @Insert(onConflict = OnConflictStrategy.IGNORE)
//    fun insertBook(bookEntity: BookEntity)

    @Query("INSERT INTO book (uri, book_name, folder, time_last_open, time_import) SELECT :uriString, :book_name, :folder, :time_last_open, :time_import")
    fun insertBook(uriString: String, book_name: String, folder: String, time_last_open: Long, time_import: Long)

    fun insertBook(bookEntity: BookEntity){
        insertBook(bookEntity.uriString, bookEntity.book_name, bookEntity.folder, bookEntity.time_last_open, bookEntity.time_import)
    }

    @Query("DELETE FROM book WHERE uri = :uriString;")
    fun deleteBookByUri(uriString: String)

    @Query("SELECT * FROM book;")
    fun getAll(): List<BookEntity>

    @Query("UPDATE book SET time_last_open = :newOpenTime WHERE uri = :uriString")
    fun updateTimeLastOpenByUri(newOpenTime: Long, uriString: String)

    @Query("UPDATE book SET folder = :newFolder WHERE uri = :uriString")
    fun updateFolderByUri(newFolder: String, uriString: String)

    @Query("UPDATE book SET book_name = :newBookName WHERE uri = :uriString")
    fun updateBookNameByUri(newBookName: String, uriString: String)

    @Query("SELECT DISTINCT folder FROM book")
    fun getAllFolders(): List<String>

    @Query("SELECT uri FROM book")
    fun getAllUris(): List<String>
}